<script setup lang="ts">
const modelValue = defineModel()
defineProps({
  disabled: {
    type: Boolean,
    default: false
  }
})
const switchOnChange = (e: Event) => {
  const input = e.target as HTMLInputElement
  modelValue.value = input.checked
}

defineOptions({
  name: 'AlexSwitch'
})
</script>

<template>
  <div class="alex-switch" style="display: flex">
    <label class="switch-label">
      <input
        :disabled
        v-model="modelValue"
        type="checkbox"
        class="switch-input"
        @change="switchOnChange"
      />
      <span class="switch-bg"></span>
      <i class="switch-icon"></i>
    </label>
  </div>
</template>

<style lang="scss" scoped>
.alex-switch {
  .switch-label {
    position: relative;
    cursor: pointer;
  }

  .switch-input {
    display: none;
    position: absolute;
    width: 0;
    height: 0;

    &:checked {
      & ~ .switch-bg {
        background-color: #3366ff;
      }
      & ~ .switch-icon {
        left: 1.49rem;
      }
    }
  }

  .switch-bg {
    display: block;
    width: 3rem;
    height: 1.5rem;
    background-color: #66758c;
    border-radius: 0.75rem;
  }

  .switch-icon {
    position: absolute;
    width: 1.5rem;
    height: 1.5rem;
    background-color: #fff;
    border-radius: 0.75rem;
    left: 0;
    top: 0;
    scale: 0.8;
    transition: left 0.2s, background-color 0.2s;
    /* 添加平滑的过渡效果 */
  }
}
</style>
